using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CMS.Presenters.Views.Template;
using CMS.Presenters.Presenters.Template;
using CMS.Core.Domain;
using CMS.Data;
using ProjectBase.Utils;

namespace CMS.Web.Template
{
    public partial class News : ViewUserControl, INewsView
    {
        NewsPresenter _presenter;
        IList<BaiViet> _lstNews;
        //int _viewId, typeId;
        string strUploadPath;

        public News()
        {
            _presenter = new NewsPresenter(this, NHibernateDaoFactory.Instance.GetBaiVietDao());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            strUploadPath = BaseUrl;
            this._presenter.GetNewsTop5();
        }        

        #region INewsView Members

        IList<BaiViet> INewsView.LstBaiViet
        {
            set
            {
                grdNews.DataSource = value;
                grdNews.DataBind();
                this._lstNews = value;
            }
            get
            {
                return this._lstNews;
            }
        }

        #endregion

        protected void grdNews_RowCreated(object sender, GridViewRowEventArgs e)
        {
            BaiViet entity = (BaiViet)e.Row.DataItem;
            if (entity != null)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    foreach (Control c in e.Row.Cells[0].Controls)
                    {
                        //the first article
                        if ((c.ClientID == "tblMainNews") && (e.Row.RowIndex == 0))
                        {
                            HtmlTable tbl = (HtmlTable)c;
                            c.Visible = true;

                            LinkButton lblTitle = (LinkButton)tbl.Rows[0].Cells[0].FindControl("lblTitle");
                            Label lblSummary = (Label)tbl.Rows[0].Cells[0].FindControl("lblSummary");
                            Image imgImage = (Image)tbl.Rows[0].Cells[0].FindControl("imgImage");
                            Label lblDate = (Label)tbl.Rows[0].Cells[0].FindControl("lblDate");
                            HtmlTable tblImg = (HtmlTable)tbl.Rows[0].Cells[0].FindControl("tblImg");

                            if (lblTitle != null)
                            {
                                lblTitle.Text = entity.TieuDe;
                                lblTitle.Attributes.Add("onclick", "return doView(\"" + this.FullBaseUrl + "?cid=" + entity.MaTheLoaiLookup.ID.ToString() + "&vid=" + entity.ID.ToString() + "\");");
                            }

                            if (lblSummary != null)
                            {
                                lblSummary.Text = entity.TomTat;
                            }

                            if ((entity.AnhDaiDien != "") && (CFile.Instance.ExistsFile("", entity.AnhDaiDien)))
                            {
                                tblImg.Visible = true;
                                imgImage.Visible = true;
                                imgImage.ImageUrl = this.FullBaseUrl + "sharedoc/" + entity.AnhDaiDien;
                            }
                            else
                            {
                                tblImg.Visible = false;
                                imgImage.Visible = false;
                            }

                        }
                        //other articles
                        else if ((c.ClientID == "tblOthersNews") && (e.Row.RowIndex > 0))
                        {
                            HtmlTable tbl = (HtmlTable)c;
                            c.Visible = true;
                            LinkButton lblTitle = (LinkButton)tbl.Rows[0].Cells[0].FindControl("lblTitleOther");
                            Label lblDateOther = (Label)tbl.Rows[0].Cells[0].FindControl("lblDateOther");

                            if (lblTitle != null)
                            {
                                lblTitle.Text = entity.TieuDe;
                                lblTitle.Attributes.Add("onclick", "return doView(\"" + this.FullBaseUrl + "?cid=" + entity.MaTheLoaiLookup.ID.ToString() + "&vid=" + entity.ID.ToString() + "\");");
                            }

                            if (lblDateOther != null)
                            {
                                lblDateOther.Text = "(" + string.Format("{0:dd/MM/yyyy}", entity.NgayCapNhat) + ")";
                            }
                        }
                    }
                }
            }
        }
    }
}